package com.smart.vista.dialog;

import com.smart.comp.TablaPadre;
import com.smart.entity.ClienteLocal;
import com.smart.entity.Clientes;
import com.smart.main.Main;
import com.smart.main.Transaccion;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class ListaClienteLocal extends javax.swing.JDialog {

    private ClienteLocal objClienteLocal;
    DefaultTableModel modeloTblClienteLocal;
    ArrayList<ClLocales> lstClienteLocal;

    public ListaClienteLocal(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        modeloTblClienteLocal = (DefaultTableModel) tblClienteLocal.getModel();
        lstClienteLocal = new ArrayList();
        anchoColumna();
        cargarLocales();
    }

    private void cargarLocales() {
        ClLocales cl;
        String HQLC = "select c.nombreComercial, cl.nombreLocal, cl.idclienteLocal from Clientes c inner join c.clienteLocals cl";
        List<Object[]> lstCliente = Transaccion.executeHQLQuery(HQLC);
        for (int i = 0; i < lstCliente.size(); i++) {
            String nombreComercial = lstCliente.get(i)[0].toString();
            String nombreLocal = lstCliente.get(i)[1].toString();
            Integer idLocal = Integer.parseInt(lstCliente.get(i)[2].toString());
            cl = new ClLocales();
            cl.setCliente(nombreComercial);
            cl.setLocal(nombreLocal);
            cl.setCodigoLocal(idLocal);
            lstClienteLocal.add(cl);
        }
    }

    public void filtrar(String local) {
        for (int i = 0; i < lstClienteLocal.size(); i++) {
            if (local.trim().equals("")) {
                Object obj[] = {lstClienteLocal.get(i).getCliente(), lstClienteLocal.get(i).getLocal(), lstClienteLocal.get(i).getCodigoLocal()};
                modeloTblClienteLocal.addRow(obj);
            } else {
                int pos2 = lstClienteLocal.get(i).getCliente().trim().toUpperCase().indexOf(local.trim().toUpperCase());
                int pos3 = lstClienteLocal.get(i).getLocal().trim().toUpperCase().indexOf(local.trim().toUpperCase());
                if (pos2 >= 0 || pos3 >= 0) {
                    Object obj[] = {lstClienteLocal.get(i).getCliente(), lstClienteLocal.get(i).getLocal(), lstClienteLocal.get(i).getCodigoLocal()};
                    modeloTblClienteLocal.addRow(obj);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblClienteLocal = new TablaPadre();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        tblClienteLocal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cliente", "Local", "Codigo Local"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblClienteLocal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblClienteLocalMouseClicked(evt);
            }
        });
        tblClienteLocal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblClienteLocalKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblClienteLocal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 722, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblClienteLocalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblClienteLocalKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            setObjClienteLocal();
        } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            this.setVisible(false);
        }
    }//GEN-LAST:event_tblClienteLocalKeyPressed

    private void tblClienteLocalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblClienteLocalMouseClicked
        if (evt.getClickCount() == 2) {
            setObjClienteLocal();
        }
    }//GEN-LAST:event_tblClienteLocalMouseClicked

    public ClienteLocal getObjClienteLocal() {
        return objClienteLocal;
    }

    private void setObjClienteLocal() {
        String codigoLocal = tblClienteLocal.getValueAt(tblClienteLocal.getSelectedRow(), 2).toString();
        String HQL = "from ClienteLocal cl where cl.idclienteLocal=" + codigoLocal;
        List<ClienteLocal> lstClientelocal = Transaccion.executeHQLQuery(HQL);
        for (ClienteLocal clienteLocal : lstClientelocal) {
            this.objClienteLocal = clienteLocal;
            this.setVisible(false);
        }
    }

    private void anchoColumna() {
        TableColumnModel modeloColumna = tblClienteLocal.getColumnModel();
        TableColumn columnaTabla = modeloColumna.getColumn(2);
        columnaTabla.setPreferredWidth(0);
        columnaTabla.setMinWidth(0);
        columnaTabla.setMaxWidth(0);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblClienteLocal;
    // End of variables declaration//GEN-END:variables

    private class ClLocales {

        private String cliente;
        private String local;
        private int codigoLocal;

        public String getCliente() {
            return cliente;
        }

        public void setCliente(String cliente) {
            this.cliente = cliente;
        }

        public String getLocal() {
            return local;
        }

        public void setLocal(String local) {
            this.local = local;
        }

        public int getCodigoLocal() {
            return codigoLocal;
        }

        public void setCodigoLocal(int codigoLocal) {
            this.codigoLocal = codigoLocal;
        }
    }
}
